package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.ManageDao;
import com.baor.domain.CooperateVo;
import com.baor.domain.ManageVo;
import com.baor.service.ManagerService;
import com.baor.utils.AjaxResult;
import com.mysql.cj.jdbc.Driver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import java.util.stream.Collectors;

import static com.baor.utils.DateUtil.now;

@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private ManageDao manageDao;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.url}")
    private String url;

    @Override
    public AjaxResult<?> managerList(ManageVo vo) {
        Map<String,Object> map = new HashMap<>();
        Page<ManageVo> page = null;
        if(vo.getPageIndex() != null){
            page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        }else{
            page = new Page<>(1,10);
        }
        map.put("pageCount", manageDao.selectPage(page, null).getPages());
        List<ManageVo> list = manageDao.selectPage(page,null).getRecords();
        List<ManageVo> list2 = list.stream().peek(x -> {
            if(x.getManageStatus().equals("0"))
                x.setManageStatus("正常");
            else
                x.setManageStatus("禁止");
        }).collect(Collectors.toList());
        map.put("list", list2);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult<?> insert(ManageVo manageVo) {
        manageVo.setManageCreatetime(now());
        manageDao.insert(manageVo);
        return AjaxResult.success(manageVo.getManageId());
    }

    @Override
    public AjaxResult<?> update(ManageVo manageVo) {
        return AjaxResult.success(manageDao.updateById(manageVo));
    }

    @Override
    public AjaxResult<?> info(Integer manageId) {
        List<ManageVo> manageVos = manageDao.selectList(new LambdaQueryWrapper<ManageVo>().eq(ManageVo::getManageId, manageId));
        List<ManageVo> list2 = manageVos.stream().peek(x -> {
            if(x.getManageStatus().equals("0"))
                x.setManageStatus("正常");
            else
                x.setManageStatus("禁止");
        }).collect(Collectors.toList());
        return  list2.size() > 0 ? AjaxResult.success(list2.get(0)) : AjaxResult.error("error：错误manageId");
    }

    @Override
    public AjaxResult<?> delete(Integer manageId) {
        return AjaxResult.success(manageDao.deleteById(manageId));
    }



    @Override
    public AjaxResult<?> search(ManageVo vo) {
        LambdaQueryWrapper<ManageVo> lqw = new LambdaQueryWrapper<>();
        Page<ManageVo> page = null;
        Map<String,Object> map = new HashMap<>();
        if(vo.getPageIndex() != null && vo.getManageNotes() != null){
            lqw.like(ManageVo::getManageNotes,vo.getManageNotes());
            page = new Page<>(vo.getPageIndex(),vo.getPageSize());
            map.put("list",manageDao.selectPage(page,lqw).getRecords());
            map.put("pageCount", manageDao.selectPage(page, lqw).getPages());
        } else if (vo.getManageNotes() != null) {
            lqw.like(ManageVo::getManageNotes,vo.getManageNotes());
            page = new Page<>(1,10);
            map.put("list", manageDao.selectPage(page,lqw).getRecords());
            map.put("pageCount", manageDao.selectPage(page, lqw).getPages());
        }else if(vo.getPageIndex() != null){
            page = new Page<>(vo.getPageIndex(),vo.getPageSize());
            map.put("list", manageDao.selectPage(page,null).getRecords());
            map.put("pageCount", manageDao.selectPage(page, null).getPages());
        }else{
            page = new Page<>(1,10);
            map.put("list", manageDao.selectPage(page,null).getRecords());
            map.put("pageCount", manageDao.selectPage(page, null).getPages());
        }
        return AjaxResult.success(map);
//        return  AjaxResult.success(manageDao.selectList(new LambdaQueryWrapper<ManageVo>().like(ManageVo::getManageNotes,manageVo.getManageNotes())));
    }

    @Override
    public AjaxResult<?> getData(List<Integer> list) throws SQLException {
        Driver driver = new Driver();
        Properties p = new Properties();
        p.setProperty("user",username);
        p.setProperty("password",password);
        Connection connect = driver.connect(url, p);
        Statement statement = connect.createStatement();
        ResultSet resultSet = null;
        List<String> msgs = new ArrayList<>();
        List<Integer> datas = new ArrayList<>();
        for ( Integer manageId : list ){
            ManageVo manageVo = manageDao.selectById(manageId);
            if ( manageVo == null ){
                msgs.add("错误的manageId:" + manageId);
                datas.add(-1);
               continue;
            }
            if ( !manageVo.getManageStatus().equals("0") ){
                msgs.add("当前控制台命令状态为禁用,请稍后再试,错误的manageId:" + manageId);
                datas.add(-1);
                continue;
            }
            String sql = manageVo.getManageParam();
            resultSet = statement.executeQuery(sql);
            int anInt = 0;
            while ( resultSet.next() ){
                anInt = resultSet.getInt(1);
            }
            msgs.add(manageVo.getManageNotes());
            datas.add(anInt);
        }
        if ( resultSet != null )
        resultSet.close();
        if ( statement != null )
        statement.close();
        if( connect != null )
        connect.close();
        Map<String,Object> res = new HashMap<>();
        res.put("msgs",msgs);
        res.put("datas",datas);
        res.put("sevenDatas",manageDao.sevenData());
        res.put("payDatas",manageDao.paysData());
        return AjaxResult.success(res);
    }






}
